package com.LeeCode;

import java.util.HashSet;
import java.util.Set;

/**
 * 每个字符最多出现两次的最长子字符串
 */

public class Code3090 {
    public static void main(String[] args) {
        String s = "eebadadbfa";
        System.out.println(new Code3090().maximumLengthSubstring(s));

    }

    public int maximumLengthSubstring(String s) {
        char[] chars = s.toCharArray();
        int ans = 0, left = 0;
        int[] cnt = new int[26];

        for (int i = 0; i < chars.length; i++) {
            int index = chars[i] - 'a';
            cnt[index]++;
            while (cnt[index] > 2) {
                cnt[chars[left] - 'a']--;
                left++;
            }
            ans = Math.max(ans, i - left + 1);
        }

        return ans;
    }
}
